﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using AdsorptionQuerier.Properties;

namespace AdsorptionQuerier
{
    public partial class FrmMain : Form
    {
        IAdsorptionDataReader m_dataReader;

        public FrmMain()
        {
            InitializeComponent();
            Settings settings = new Settings();
            switch (settings.DataType)
            {
                case 1:
                    m_dataReader = new AdsorptionSqlReader(settings.DataFile);
                    break;
                default:
                    Debug.Assert(false);
                    break;
            }

            foreach (string sUser in m_dataReader.GetUsers())
            {
                cmbUser.Items.Add(sUser);
            }
            foreach (string sItem in m_dataReader.GetItems())
            {
                cmbItem.Items.Add(sItem);
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cmbItem.Text) || string.IsNullOrEmpty(cmbUser.Text))
            {
                cmbUser.Focus();
                return;
            }

            dgMain.Rows.Clear();
            dgMain.Columns.Clear();
            
            foreach (string sColName in m_dataReader.GetDataColumns())
            {
                dgMain.Columns.Add(sColName, sColName);
            }

            foreach (WeightedItem item in m_dataReader.Read(cmbUser.Text, cmbItem.Text))
            {
                if (((AdsorptionSqlReader)m_dataReader).HasItemData)
                {
                    dgMain.Rows.Add(item.Id, item.Weight,
                        item.ItemName, item.ReleaseDate, item.IMDbLink);
                }
                else
                {
                    dgMain.Rows.Add(item.Id, item.Weight);
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
